home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Tools - Objects / MacApp / MacApp 2.0 CD Release / MacApp® 2.0 Tutorial / Chapter 12 / UIconEdit.inc1.p < prev    next >
Encoding:
Text File  |  1990-03-27  |  12.0 KB  |  445 lines  |  [TEXT/MPS ]

  1. {Copyright © 1989 by Apple Computer, Inc.  All rights reserved.}
  2.  
  3.  
  4.  
  5. CONST
  6.     kIconHBits =        32;                            { Number of horizontal bits in a bitmap.}
  7.     kIconVBits =        32;                            { Number of vertical bits in a bitmap.    }
  8.  
  9.     kIconSizeInBytes =    kIconHBits * kIconVBits DIV 8;    { Number of bytes in an bitmap.        }
  10.     kIconSizeInLongs =    kIconSizeInBytes DIV 4;        { Number of long words in a bitmap.        }
  11.     kMaxLong =            kIconSizeInLongs - 1;        { Max. addressable long word in bitmap.    }
  12.     
  13.     
  14.     { Resource identifiers }
  15.     
  16.     kSeedIconId =        1000;                        { Id of the seed icon resource.            }
  17.  
  18.     kIconWindowId =        1000;                        { Id of the icon window 'view' resource.}
  19.     kIconViewId =        1001;                        { Id of the TIconView resource.            }
  20.  
  21.     
  22.     { Constants for TIconView }
  23.  
  24.     kDefaultMagnification =    7;                        { Default icon magnification.            }
  25.     kBorder =                5;                        { Border in which to inset drawing.        }
  26.  
  27.     { Command Numbers }
  28.  
  29.     cZoomIn =            1000;                        { Zoom In menu command.                    }
  30.     cZoomOut =            1001;                        { Zoom Out menu command.                }
  31.  
  32.  
  33.  
  34.  
  35.  
  36. TYPE
  37.     LongArrayHdl =        ^LongArrayPtr;
  38.     LongArrayPtr =        ^LongArray;
  39.     LongArray =            ARRAY [0..kMaxLong] OF LONGINT;
  40.  
  41.  
  42.  
  43. {-------------------------------------------------------------------------------------------}
  44. {--------------------------------TIconApplication methods-----------------------------------}
  45. {-------------------------------------------------------------------------------------------}
  46.  
  47. PROCEDURE TIconApplication.IIconApplication(iconFileType: OSType);
  48.  
  49. VAR anIconView : TIconView;
  50.  
  51. BEGIN
  52.     IApplication(iconFileType);
  53.     
  54.     if gCreateWithTemplates then begin  { Make sure the linker doesn't strip out view code. }
  55.         New(anIconView);
  56.     end;
  57. END;
  58.  
  59.  
  60.  
  61. {-------------------------------------------------------------------------------------------}
  62.  
  63. FUNCTION  TIconApplication.DoMakeDocument(itsCmdNumber: CmdNumber): TDocument; OVERRIDE;
  64.  
  65. VAR
  66.     anIconDocument:        TIconDocument;
  67.  
  68. BEGIN
  69.     New(anIconDocument);                            { Create a TIconDocument object.        }
  70.     FailNIL(anIconDocument);                        { Make sure we were successful.            }
  71.     anIconDocument.IIconDocument;                    { Initialize it.                        }
  72.  
  73.     DoMakeDocument := anIconDocument;                { Return a reference to the document.    }
  74. END;
  75.     
  76.     
  77.     
  78.     
  79. {-------------------------------------------------------------------------------------------}
  80. {----------------------------------TIconDocument methods------------------------------------}
  81. {-------------------------------------------------------------------------------------------}
  82.  
  83. PROCEDURE TIconDocument.IIconDocument;
  84.  
  85. VAR anIconBitMap : TIconBitMap;
  86.  
  87. BEGIN
  88.     fIconBitMap := NIL;                                { Set this to NIL so that if IDocument    }
  89.                                                     { fails, TIconDocument.Free works okay.    }
  90.  
  91.     IDocument(kFileType,                             { This document's file type.            }
  92.               kSignature,                             { This document's creator.                }
  93.               kUsesDataFork,                         { This document does use the data fork    }
  94.               NOT kUsesRsrcFork,                    { …but doesn't use the resource fork.    }
  95.               NOT kDataOpen,                        { We don't want the data fork kept open    }
  96.               NOT kRsrcOpen);                        { …nor the resource fork.                }
  97.  
  98.     New(anIconBitMap);                                { Allocate a new icon bitmap.            }
  99.     FailNil(anIconBitMap);                            { Fail if we can't allocate the handle.    }
  100.     anIconBitMap.IIconBitMap;                        { Initialize it.                        }
  101.  
  102.     fIconBitMap := anIconBitMap;                    { Store a reference to it in a field.    }
  103. END;
  104.  
  105.  
  106.  
  107. {-------------------------------------------------------------------------------------------}
  108.  
  109. PROCEDURE TIconDocument.DoInitialState; OVERRIDE;
  110.     { This method is called to set the document's data to the "new" state, as when the user    }
  111.     { chooses to open a new document instead of an existing one.  We set the value of the     }
  112.     { document's icon bit map to that of a "seed" icon in our resource file.  That way we     }
  113.     { can the document's initial state simply by changing the "seed" icon resource.            }
  114.  
  115. VAR
  116.     seedIcon:        Handle;
  117.  
  118. BEGIN
  119.     seedIcon := GetIcon(kSeedIconId);                { Get the seed icon resource.            }
  120.     FailNilResource(seedIcon);
  121.     fIconBitMap.SetIconBitMap(seedIcon)
  122. END;
  123.  
  124.  
  125.  
  126. {-------------------------------------------------------------------------------------------}
  127.  
  128. PROCEDURE TIconDocument.Free; OVERRIDE;
  129.     
  130. BEGIN
  131.     FreeIfObject(fIconBitMap);                        { Dispose of the icon object if non-Nil.}
  132.  
  133.     INHERITED Free;
  134. END;
  135.  
  136.  
  137.  
  138. {-------------------------------------------------------------------------------------------}
  139.  
  140. PROCEDURE TIconDocument.DoMakeViews (forPrinting: BOOLEAN); OVERRIDE;
  141.  
  142. VAR
  143.     aWindow:  TWindow;
  144.  
  145. BEGIN
  146.     aWindow := NewTemplateWindow(kIconWindowId, SELF);        { Create the view hierarchy }
  147. END;
  148.  
  149.  
  150.     
  151. {-------------------------------------------------------------------------------------------}
  152.  
  153. PROCEDURE TIconDocument.Fields (PROCEDURE DoToField (fieldName: Str255;
  154.                                                    fieldAddr: Ptr;
  155.                                                    fieldType: INTEGER)); OVERRIDE;
  156.  
  157. BEGIN
  158.     DoToField('TIconDocument', NIL, bClass);
  159.     DoToField('fIconBitMap', @fIconBitMap, bObject);
  160.  
  161.     INHERITED Fields(DoToField);
  162. END;
  163.  
  164.  
  165.  
  166. {-------------------------------------------------------------------------------------------}
  167. {------------------------------------TIconView methods--------------------------------------}
  168. {-------------------------------------------------------------------------------------------}
  169.  
  170.  
  171. PROCEDURE TIconView.IRes (itsDocument: TDocument; itsSuperView: TView; VAR itsParams: Ptr);
  172.  
  173. BEGIN
  174.     INHERITED IRes(itsDocument, itsSuperView, itsParams);
  175.  
  176.     fMagnification := kDefaultMagnification;
  177.     fIconDocument := TIconDocument(itsDocument);
  178. END;
  179.  
  180.  
  181.  
  182. {-------------------------------------------------------------------------------------------}
  183.  
  184. PROCEDURE TIconView.CalcMinSize (VAR minSize: VPoint); OVERRIDE;
  185.  
  186. BEGIN
  187.     minSize.h := kIconHBits * fMagnification + kBorder + kBorder;
  188.     minSize.v := kIconVBits * fMagnification + kBorder + kBorder;
  189. END;
  190.  
  191.  
  192.  
  193.  
  194. {-------------------------------------------------------------------------------------------}
  195.  
  196. PROCEDURE TIconView.Draw (area: Rect); OVERRIDE;
  197.  
  198. VAR
  199.     drawingRect:    Rect;
  200.  
  201. BEGIN
  202.     SetRect(drawingRect, kBorder, kBorder,
  203.                          kBorder + (kIconHBits * fMagnification),
  204.                          kBorder + (kIconVBits * fMagnification));
  205.  
  206.     fIconDocument.fIconBitMap.Draw(drawingRect);    
  207. END;
  208.  
  209.  
  210. {-------------------------------------------------------------------------------------------}
  211.  
  212. PROCEDURE TIconView.DoSetupMenus; OVERRIDE;
  213.  
  214. BEGIN
  215.     INHERITED DoSetupMenus;                            { Set up inherited menus.                }
  216.  
  217.     Enable(cZoomIn, TRUE);                            { Can always zoom in.                    }
  218.     Enable(cZoomOut, fMagnification > 1);            { Can zoom out if not at smallest size.    }
  219. END;
  220.  
  221.  
  222.  
  223. {-------------------------------------------------------------------------------------------}
  224.  
  225. FUNCTION TIconView.DoMenuCommand (aCmdNumber: CmdNumber): TCommand; OVERRIDE;
  226.  
  227. BEGIN
  228.     DoMenuCommand := gNoChanges;                    { Initialize result of DoMenuCommand.    }
  229.  
  230.     CASE aCmdNumber OF                                { Decide if this command is ours…        }
  231.  
  232.         cZoomIn:
  233.             SetMagnification(fMagnification + 2);    { Increase size of each bit by 2 pixels.}
  234.  
  235.         cZoomOut:
  236.             SetMagnification(fMagnification - 2);    { Decrease size of each bit by 2 pixels.}
  237.  
  238.         OTHERWISE                                    { Otherwise, let someone else handle it.}
  239.             DoMenuCommand := INHERITED DoMenuCommand(aCmdNumber);
  240.     END;
  241. END;
  242.  
  243.  
  244.  
  245. {-------------------------------------------------------------------------------------------}
  246.  
  247. PROCEDURE TIconView.SetMagnification (magnification: INTEGER);
  248.  
  249. BEGIN
  250.     fMagnification := Max(1, magnification);        { Set the new magnification.            }
  251.     AdjustSize;                                        { Magnification affects the view's size.}
  252.     ForceRedraw;                                    { Force the view to be entirely redrawn.}
  253. END;
  254.  
  255.  
  256.  
  257.  
  258. {-------------------------------------------------------------------------------------------}
  259.  
  260. PROCEDURE TIconView.Fields (PROCEDURE DoToField (fieldName: Str255;
  261.                                                  fieldAddr: Ptr;
  262.                                                  fieldType: INTEGER)); OVERRIDE;
  263.                                                  
  264. BEGIN
  265.     DoToField('TIconView', NIL, bClass);
  266.     DoToField('fIconDocument', @fIconDocument, bObject);
  267.     DoToField('fMagnification', @fMagnification, bInteger);
  268.     
  269.     INHERITED Fields(DoToFIeld);
  270. END;
  271.  
  272.  
  273. {-------------------------------------------------------------------------------------------}
  274. {-----------------------------------TIconBitMap methods-------------------------------------}
  275. {-------------------------------------------------------------------------------------------}
  276.  
  277. PROCEDURE TIconBitMap.IIconBitMap;
  278.  
  279. BEGIN
  280.     fDataHandle := NewPermHandle(kIconSizeInBytes);    { Allocate a handle for the bitmap.        }
  281.     FailNil(fDataHandle);                            { Fail if we can't allocate the handle.    }
  282. END;
  283.  
  284.  
  285.  
  286. {-------------------------------------------------------------------------------------------}
  287.  
  288. PROCEDURE TIconBitMap.Free; OVERRIDE;
  289.  
  290. BEGIN
  291.     DisposIfHandle(fDataHandle);                    { dispose of icon data.                    }
  292. END;
  293.  
  294.  
  295.  
  296. {-------------------------------------------------------------------------------------------}
  297.  
  298. PROCEDURE TIconBitMap.SetIconBitMap(theBitMap : Handle);
  299.  
  300. BEGIN
  301.     BlockMove(theBitMap^, fDataHandle^,                { …then copy it into the document's     }
  302.                 kIconSizeInBytes)                    { …icon bitmap.                            }
  303. END;
  304.             
  305.             
  306.  
  307. {-------------------------------------------------------------------------------------------}
  308.  
  309. PROCEDURE TIconBitMap.Clear;
  310.  
  311. VAR
  312.     iconAsLongArray:    LongArrayHdl;
  313.     i:                    INTEGER;
  314.  
  315. BEGIN
  316.     iconAsLongArray := LongArrayHdl(fDataHandle);    { Cast to array of longints.            }
  317.  
  318.     FOR i := 0 TO kMaxLong DO                        { Clear the bits 32 at a time.            }
  319.         iconAsLongArray^^[i] := 0;
  320. END;
  321.  
  322.  
  323.  
  324. {-------------------------------------------------------------------------------------------}
  325.  
  326. PROCEDURE TIconBitMap.Invert;
  327.  
  328. VAR
  329.     iconAsLongArray:    LongArrayHdl;
  330.     i:                    INTEGER;
  331.  
  332. BEGIN
  333.     iconAsLongArray := LongArrayHdl(fDataHandle);    { Cast to array of longints.            }
  334.  
  335.     FOR i := 0 TO kMaxLong DO                        { Invert the bits 32 at a time.            }
  336.         iconAsLongArray^^[i] := BNOT(iconAsLongArray^^[i]);
  337. END;
  338.  
  339.  
  340.  
  341. {-------------------------------------------------------------------------------------------}
  342.  
  343. PROCEDURE TIconBitMap.IconBitToWordBit (iconBit: Point; VAR word, bit: INTEGER);
  344.     { This converts the given icon bit to a word and bit in an array of long words. }
  345.  
  346. VAR
  347.     bitNumber:        INTEGER;
  348.  
  349. BEGIN
  350.     bitNumber := iconBit.v * kIconVBits + iconBit.h;
  351.     word := bitNumber DIV 32;
  352.     bit := 31 - (bitNumber MOD 32);
  353. END;
  354.  
  355.  
  356.  
  357. {-------------------------------------------------------------------------------------------}
  358.  
  359. FUNCTION  TIconBitMap.GetBit (iconBit: Point): BOOLEAN;
  360.     { Returns the state of the given bit in the icon being drawn. }
  361.  
  362. VAR
  363.     word:            INTEGER;
  364.     bitInWord:        INTEGER;
  365.  
  366. BEGIN
  367.     IconBitToWordBit(iconBit, word, bitInWord);
  368.  
  369.     GetBit := BTst(LongArrayHdl(fDataHandle)^^[word], bitInWord);
  370. END;
  371.  
  372.  
  373.  
  374. {-------------------------------------------------------------------------------------------}
  375.  
  376. PROCEDURE TIconBitMap.SetBit (iconBit: Point; turnBitOn: BOOLEAN);
  377.     { Set the state of the given bit in the icon being drawn. }
  378.  
  379. VAR
  380.     word:            INTEGER;
  381.     bitInWord:        INTEGER;
  382.  
  383. BEGIN
  384.     IconBitToWordBit(iconBit, word, bitInWord);
  385.  
  386.     {$H-}                                            { So the compiler thinks this is unsafe.}
  387.     IF turnBitOn THEN
  388.         BSet(LongArrayHdl(fDataHandle)^^[word], bitInWord)
  389.     ELSE
  390.         BClr(LongArrayHdl(fDataHandle)^^[word], bitInWord);
  391.     {$H+}
  392. END;
  393.  
  394.  
  395.  
  396. {-------------------------------------------------------------------------------------------}
  397.  
  398. FUNCTION TIconBitMap.Copy: TIconBitMap;
  399.  
  400. VAR
  401.     copyOfIcon:        TIconBitMap;
  402.  
  403. BEGIN
  404.     New(copyOfIcon);                                { Create a TIcon object.                }
  405.     FailNIL(copyOfIcon);                            { Make sure we were successful.            }
  406.     copyOfIcon.IIconBitMap;                            { Initialize it.                        }
  407.     copyOfIcon.SetIconBitMap(fDataHandle);            { Copy the data.                        }
  408.     Copy := copyOfIcon;                                { Return a reference to the new handle.    }
  409. END;
  410.  
  411.  
  412.             
  413. {-------------------------------------------------------------------------------------------}
  414.  
  415. PROCEDURE TIconBitMap.Draw (area: Rect);
  416.  
  417. BEGIN
  418.     PlotIcon(area, fDataHandle);    
  419. END;
  420.  
  421.  
  422.  
  423. {-------------------------------------------------------------------------------------------}
  424.  
  425. PROCEDURE TIconBitMap.CopyDataTo (anIcon: TIconBitMap);
  426.  
  427. BEGIN
  428.     anIcon.SetIconBitMap(fDataHandle);                { Copy data to the new icon.            }
  429. END;
  430.  
  431.  
  432. {-------------------------------------------------------------------------------------------}
  433.  
  434. PROCEDURE TIconBitMap.Fields (PROCEDURE DoToField (fieldName: Str255;
  435.                                                    fieldAddr: Ptr;
  436.                                                    fieldType: INTEGER)); OVERRIDE;
  437.  
  438. BEGIN
  439.     DoToField('TIconBitMap', NIL, bClass);
  440.     DoToField('fDataHandle', @fDataHandle, bHandle);
  441.  
  442.     INHERITED Fields(DoToField);
  443. END;
  444.  
  445.